package com.xie.smfs.entity;

import net.minecraft.entity.EntityType;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.DamageType;
import net.minecraft.entity.damage.DamageTypes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;

import java.util.ArrayList;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import java.util.List;

public class FoodGhostEntity extends GhostEntity {
    private static final boolean HAS_GHOST_DOMAIN = true;
    private static final int GHOST_DOMAIN_LEVEL = 3; // 高级鬼域
    private static final double GHOST_DOMAIN_RADIUS = 32.0;
    private static final char TERROR_LEVEL = 'A'; // A级恐怖

    public FoodGhostEntity(EntityType<? extends GhostEntity> entityType, World world) {
        super(entityType, world, HAS_GHOST_DOMAIN, GHOST_DOMAIN_LEVEL, GHOST_DOMAIN_RADIUS, TERROR_LEVEL);
        this.ghostLevel = 3; // 设置鬼等级
        this.attackCooldown = 60; // 较长攻击冷却
    }

    @Override
    protected boolean shouldAttackPlayer(PlayerEntity player) {
        // 杀人规则：攻击范围内且正在使用食物的玩家
        return player.isUsingItem() && player.getActiveItem().isFood();
    }

    @Override
    protected void executeAttack(PlayerEntity player) {
        // 吃东西杀人规则：鬼域内吃东西的玩家会直接饥饿死去。
        // 瞬移到玩家面前
        this.teleport(player.getX(), player.getY(), player.getZ());

        // 获取 mob 伤害类型的注册表项
        RegistryEntry<DamageType> mobDamageType = this.getWorld().getRegistryManager().get(RegistryKeys.DAMAGE_TYPE).getEntry(RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier("smfs", "ghost"))).orElseThrow(() -> new IllegalStateException("MOB_ATTACK damage type not found"));
        // 创建伤害源
        DamageSource damageSource = new DamageSource(mobDamageType, this);
        player.damage(damageSource, 1000.0f);
    }

    @Override
    public void tick() {
        super.tick();
        if (isHasGhostDomain() && ghostDomainEnabled && this.age % 20 == 0) {
            // 每20ticks(1秒)检查一次鬼域内玩家
            List<PlayerEntity> players = new ArrayList<>();
            MinecraftServer server = getWorld().getServer();
            if (server != null) {
                PlayerManager playerManager = server.getPlayerManager();
                for (PlayerEntity player : playerManager.getPlayerList()) {
                    double distanceSquared = (player.getX() - this.getX()) * (player.getX() - this.getX()) + (player.getY() - this.getY()) * (player.getY() - this.getY()) + (player.getZ() - this.getZ()) * (player.getZ() - this.getZ());
                    if (distanceSquared <= GHOST_DOMAIN_RADIUS * GHOST_DOMAIN_RADIUS && player.isAlive()) {
                        players.add(player);
                    }
                }
            }
            for (PlayerEntity player : players) {
                // 施加1级饥饿效果，持续100ticks(5秒)
                player.addStatusEffect(new StatusEffectInstance(StatusEffects.HUNGER, 100, 0), this);
            }
        }
    }
}